// pages/goodsFabu/putongFabu/putongFabu.js
// 日期
const app = getApp();
var date = new Date()
const years = []
const months = []
const days = []
const hours = []
const minutes = []
const seconds = []
for (let i = 1990; i <= date.getFullYear() + 1; i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}

for (let i = 0; i <= 23; i++) {
  hours.push(i)
}
for (let i = 0; i <= 59; i++) {
  minutes.push(i)
  seconds.push(i)
}
Page({
  data: {
    zanshiName: '', //暂时
    anshiId: '', //暂时
    showAnceng1: false, //暗层
    shopfenlModal: false, //店铺分类
    yijiModel: false, //一级分类
    erjiModel: false, //二级分类
    danweiModel: false, //单位
    addshopfenlModal: false,
    headIndex: 0,

    years: years, //日期初始化
    year1: '',
    year2: '',
    year3: '',
    months: months,
    month1: '',
    month2: '',
    month3: '',
    days: days,
    day1: '',
    day2: '',
    day3: '',
    hours: hours,
    hour1: '',
    hour2: '',
    hour3: '',
    minutes: minutes,
    minute1: '',
    minute2: '',
    minute3: '',
    seconds: seconds,
    second1: '',
    second2: '',
    second3: '',
    valueRiqi: '',
    valueShijian: '',
    showAnceng: false, //暗层
    goodsModalone: false, //商品开始时间年月日弹窗
    goodsModaltwo: false, //商品开始时间时分秒弹窗
    zanshistartTime: '', //点击下一步时暂时年月日时间
    goodsstartTimes: '', //选择后的代销结束日期
    startTimestr: '', //代销结束日期字符串
    goodssendTimes: '', //零售结束日期
    endTimestr: '', //零售结束日期字符串
    sumEndTime: '', //结算结束日期
    sumEndTimestr: '', //结算结束日期字符串
    startorend: 1, //1是代销结束时间，2零售结束时间3结算结束日期


    region: [], //市区选择器
    array: [], //商品单位
    shuxingFenleiarray: [], //商品属性分类列表
    yijiFenlei: '', //一级分类ID
    erjiFenlei: '', //二级分类ID
    danweis: '', //单位ID
    shuxing: '', //属性ID
    index: '', //商品单位下标
    sumfenlei: '', //一级二级总分类
    yijiindex: '', //一级分类下标
    erjiindex: '', //二级分类下标
    shuxingindex: '', //属性分类下标
    goodsValName: '', //商品名称
    imgURL: '', //信息封面图
    qibunum: '', //起步量
    shouyibili: '', //收益比例
    guigelist: [], //规格列表
    imglistobg: '', //上传的单个商品主图
    goodsZhutu: [], //商品主图列表
    xiangQingimg: '',
    hiddenmodalput: true, //新建商品属性显示
    name: "", //新添加的分类
    provinceId: '', //省ID
    cityId: '', //市ID
    townId: '', //区县ID,
    goodsId: '', //修改必填
    actId: '', //修改必填
  },
  // 去选择区域
  toaddquyu() {
    wx.navigateTo({
      url: '/pages/goods/page/addtownid/addtownid?townList=' + JSON.stringify(this.data.region)
      // ?index=0&guigelist=' + JSON.stringify(this.data.guigelist),
    })
  },
  //起步量
  qibuliang(e) {
    this.setData({
      qibunum: e.detail.value
    })
  },
  //收益比例
  shouyibili(e) {
    this.setData({
      shouyibili: e.detail.value
    })
  },
  shouyibiliShijiao(e) {
    let that = this
    let values = e.detail.value
    if (values * 1 >= 1) {
      wx.showToast({
        title: '分成比例为小于1的小数',
        icon: 'none'
      })
      that.setData({
        shouyibili: ''
      })
      return
    }
    if (values.indexOf('.') != -1) {
      values = (values * 1).toFixed(2)
    }
    if (values == 'NaN') {
      wx.showToast({
        title: '请输入正确的代销分成比例',
        icon: 'none'
      })
      values = ''
    }
    that.setData({
      shouyibili: values
    })
  },
  // 获取平台店铺分类
  getshoptypelist() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res, 55)
        if (res.data.result == 0) {
          that.setData({
            shuxingFenleiarray: res.data.goodsTypes
          })
        }
      }
    });
  },

  // 获取商品的计量单位 
  getdanwei() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getGoodUnitData"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        // setTimeout(function () {
        //   wx.hideLoading()
        // }, 0)
        // setTimeout(function () {
        //   wx.showToast({
        //     title: res.data.resultNote,
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }, 0)
        console.log(res)
        that.setData({
          array: res.data.unitList
        })
        // if (res.data.result == 0) {
        //   setTimeout(function () {
        //     wx.navigateBack({
        //       delta: 1
        //     })
        //   }, 1500)
        // }
      }
    });
  },
  close_dingdanTime() {
    let that = this
    that.setData({
      showAnceng: false, //暗层
      goodsModalone: false, //商品开始时间年月日弹窗
      goodsModaltwo: false, //商品开始时间时分秒弹窗
    })
  },
  // 日期切换
  riqiChange: function(e) {
    const val = e.detail.value
    let that = this
    if (that.data.startorend == 1) {
      this.setData({
        year1: this.data.years[val[0]],
        month1: this.data.months[val[1]],
        day1: this.data.days[val[2]]
      })
    } else if (that.data.startorend == 2) {
      this.setData({
        year2: this.data.years[val[0]],
        month2: this.data.months[val[1]],
        day2: this.data.days[val[2]]
      })
    } else if (that.data.startorend == 3) {
      this.setData({
        year3: this.data.years[val[0]],
        month3: this.data.months[val[1]],
        day3: this.data.days[val[2]]
      })
    }

  },
  // 时间切换
  shijianChange: function(e) {
    const val = e.detail.value
    let that = this
    if (that.data.startorend == 1) {
      this.setData({
        hour1: this.data.hours[val[0]],
        minute1: this.data.minutes[val[1]],
        // second1: this.data.seconds[val[2]]
      })
    } else if (that.data.startorend == 2) {
      this.setData({
        hour2: this.data.hours[val[0]],
        minute2: this.data.minutes[val[1]],
        // second2: this.data.seconds[val[2]]
      })
    }
    if (that.data.startorend == 3) {
      this.setData({
        hour3: this.data.hours[val[0]],
        minute3: this.data.minutes[val[1]],
        // second3: this.data.seconds[val[2]]
      })
    }

  },
  // 统计开始时间
  tongJiStart(e) {
    console.log(e.currentTarget.dataset.startorend)
    let that = this
    let dates = new Date()
    that.setData({
      year1: dates.getFullYear(),
      year2: dates.getFullYear(),
      year3: dates.getFullYear(),

      month1: dates.getMonth() + 1,
      month2: dates.getMonth() + 1,
      month3: dates.getMonth() + 1,

      day1: dates.getDate(),
      day2: dates.getDate(),
      day3: dates.getDate(),

      hour1: dates.getHours(),
      hour2: dates.getHours(),
      hour3: dates.getHours(),

      minute1: dates.getMinutes(),
      minute2: dates.getMinutes(),
      minute3: dates.getMinutes(),

      // second1: dates.getSeconds(),
      // second2: dates.getSeconds(),
      // second3: dates.getSeconds(),
      valueRiqi: [dates.getFullYear() - 1990, dates.getMonth(), dates.getDate() - 1],
      valueShijian: [dates.getHours(), dates.getMinutes(), '00'],
    })
    let valueRiqi, valueShijian
    if (e.currentTarget.dataset.startorend == 1) {
      valueRiqi = [(that.data.year1 - 1990), that.data.month1 - 1, that.data.day1 - 1]
      valueShijian = [that.data.hour1, that.data.minute1, that.data.second1]
    } else if (e.currentTarget.dataset.startorend == 2) {
      valueRiqi = [(that.data.year2 - 1990), that.data.month2 - 1, that.data.day2 - 1]
      valueShijian = [that.data.hour2, that.data.minute2, that.data.second2]
    } else if (e.currentTarget.dataset.startorend == 3) {
      valueRiqi = [(that.data.year3 - 1990), that.data.month3 - 1, that.data.day3 - 1]
      valueShijian = [that.data.hour3, that.data.minute3, that.data.second3]
    }
    this.setData({
      goodsModalone: true,
      showAnceng: true,
      startorend: e.currentTarget.dataset.startorend,
      valueRiqi: valueRiqi,
      valueShijian: valueShijian,
      // year: e.currentTarget.dataset.year * 1,
      // month: e.currentTarget.dataset.month * 1,
    })
  },
  // 统计下一步时间
  xiayiBu: function() {
    let that = this
    let times = {}
    if (that.data.startorend == 1) {
      times.year = that.data.year1
      if (that.data.month1 * 1 < 10) {
        times.month = '0' + that.data.month1
      } else {
        times.month = that.data.month1
      }
      if (that.data.day1 * 1 < 10) {
        times.day = '0' + that.data.day1
      } else {
        times.day = that.data.day1
      }
    } else if (that.data.startorend == 2) {
      times.year = that.data.year2
      if (that.data.month2 * 1 < 10) {
        times.month = '0' + that.data.month2
      } else {
        times.month = that.data.month2
      }
      if (that.data.day2 * 1 < 10) {
        times.day = '0' + that.data.day2
      } else {
        times.day = that.data.day2
      }
    } else if (that.data.startorend == 3) {
      times.year = that.data.year3
      if (that.data.month3 * 1 < 10) {
        times.month = '0' + that.data.month3
      } else {
        times.month = that.data.month3
      }
      if (that.data.day3 * 1 < 10) {
        times.day = '0' + that.data.day3
      } else {
        times.day = that.data.day3
      }
    }

    this.setData({
      goodsModalone: false,
      goodsModaltwo: true,
      zanshistartTime: times
    })
  },
  // 统计确定时间
  quedingEnd: function() {
    let that = this
    let times = that.data.zanshistartTime
    if (that.data.startorend == 1) {
      if (that.data.hour1 * 1 < 10) {
        times.hour = '0' + that.data.hour1
      } else {
        times.hour = that.data.hour1
      }
      if (that.data.minute1 * 1 < 10) {
        times.minute = '0' + that.data.minute1
      } else {
        times.minute = that.data.minute1
      }
      // if (that.data.second1 * 1 < 10) {
      //   times.second = '0' + that.data.second1
      // } else {
      //   times.second = that.data.second1
      // }
    } else if (that.data.startorend == 2) {
      if (that.data.hour2 * 1 < 10) {
        times.hour = '0' + that.data.hour2
      } else {
        times.hour = that.data.hour2
      }
      if (that.data.minute2 * 1 < 10) {
        times.minute = '0' + that.data.minute2
      } else {
        times.minute = that.data.minute2
      }
      // if (that.data.second2 * 1 < 10) {
      //   times.second = '0' + that.data.second2
      // } else {
      //   times.second = that.data.second2
      // }
    } else if (that.data.startorend == 3) {
      if (that.data.hour3 * 1 < 10) {
        times.hour = '0' + that.data.hour3
      } else {
        times.hour = that.data.hour3
      }
      if (that.data.minute3 * 1 < 10) {
        times.minute = '0' + that.data.minute3
      } else {
        times.minute = that.data.minute3
      }
      // if (that.data.second3 * 1 < 10) {
      //   times.second = '0' + that.data.second3
      // } else {
      //   times.second = that.data.second3
      // }
    }

    let timestr = times.year + '-' + times.month + '-' + times.day + ' ' + times.hour + ':' + times.minute + ':00'
    if (that.data.startorend == 1) {
      this.setData({
        showAnceng: false,
        goodsModaltwo: false,
        goodsstartTimes: times,
        startTimestr: timestr
      })
    } else if (that.data.startorend == 2) {
      this.setData({
        showAnceng: false,
        goodsModaltwo: false,
        goodssendTimes: times,
        endTimestr: timestr
      })
    } else if (that.data.startorend == 3) {
      this.setData({
        showAnceng: false,
        goodsModaltwo: false,
        sumEndTime: times,
        sumEndTimestr: timestr
      })
    }
  },
  //去添加规格界面
  addGuige() {
    wx.navigateTo({
      url: '/pages/goods/page/tianjiaGuige/putongGuige/putongGuige?index=5&guigelist=' + JSON.stringify(this.data.guigelist),
    })
  },
  // 修改规格
  toxiugai(e) {
    console.log(e)
    wx.navigateTo({
      url: '/pages/goods/page/tianjiaGuige/putongGuige/putongGuige?index=5&guigelist=' + JSON.stringify(this.data.guigelist) + '&listitem=' + e.currentTarget.dataset.item,
    })
  },
  // 添加商品封面图：
  addFengmian() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper',
    })
  },
  // 重置信息
  chongzhiInfoo() {
    // console.log('重置信息')
    let that = this
    wx.showModal({
      title: '是否重置信息',
      success: function(res) {
        if (res.confirm) {
          that.setData({
            region: [], //市区选择器
            index: '', //商品单位下标
            yijiFenlei: '',
            erjiFenlei: '',
            danweis: '',
            shuxing: '',
            index: '',
            yijiindex: '', //一级分类下标
            erjiindex: '', //二级分类下标
            shuxingindex: '', //属性分类下标
            goodsValName: '', //商品名称
            imgURL: '', //商品封面图
            guigelist: [],
            imglistobg: '',
            goodsZhutu: [],
            xiangQingimg: '',
            name: '',
            qibunum: '', //起步量
            shouyibili: '', //收益比例
          })
        } else {
          console.log('点击取消回调')
        }
      }
    })

  },
  // 确认提交
  tiJiaoInfo() {
    let that = this
    if (!that.data.goodsValName) {
      wx.showToast({
        title: '请输入商品名称',
        icon: 'none'
      })
      return
    }
    if (!that.data.shuxing) {
      wx.showToast({
        title: '请选择店铺商品属性分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.yijiFenlei) {
      wx.showToast({
        title: '请选择一级分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.erjiFenlei) {
      wx.showToast({
        title: '请选择二级分类',
        icon: 'none'
      })
      return
    }
    if (!that.data.danweis) {
      wx.showToast({
        title: '请选择商品单位',
        icon: 'none'
      })
      return
    }
    if (!that.data.imgURL) {
      wx.showToast({
        title: '请上传封面图',
        icon: 'none'
      })
      return
    }
    if (that.data.region.length < 1) {
      wx.showToast({
        title: '请选择销售区域',
        icon: 'none'
      })
      return
    }
    let tlist = []
    let list = that.data.region
    for (var index in list) {
      tlist.push(list[index].townId)
    }
    if (!that.data.qibunum) {
      wx.showToast({
        title: '请输入起步量',
        icon: 'none'
      })
      return
    }
    if (!that.data.shouyibili) {
      wx.showToast({
        title: '请输入代销收益分成比例',
        icon: 'none'
      })
      return
    }
    if (that.data.goodsZhutu.length < 1) {
      wx.showToast({
        title: '请上传商品主图',
        icon: 'none'
      })
      return
    }
    if (!that.data.xiangQingimg) {
      wx.showToast({
        title: '请上传商品详情图',
        icon: 'none'
      })
      return
    }
    if (that.data.guigelist.length < 1) {
      wx.showToast({
        title: '请至少添加一种规格',
        icon: 'none'
      })
      return
    }
    if (!that.data.startTimestr) {
      wx.showToast({
        title: '请选择代销结束时间',
        icon: 'none'
      })
      return
    }
    if (!that.data.endTimestr) {
      wx.showToast({
        title: '请选择代销零售结束时间',
        icon: 'none'
      })
      return
    }
    if (!that.data.sumEndTimestr) {
      wx.showToast({
        title: '请选择代销结算结束时间',
        icon: 'none'
      })
      return
    }
    let gActBean = {
      actId: that.data.actId,
      endTtime: that.data.startTimestr,
      saleeTime: that.data.endTimestr,
      accteTime: that.data.sumEndTimestr,
      minbuynum: parseInt(that.data.qibunum)
    }
    let type
    if (that.data.goodsId) {
      type = 2
    } else {
      type = 1
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交？',
      success (res) {
      if (res.confirm) {
      console.log('用户点击确定')
      wx.request({
        url: app.globalData.myurl,
        data: {
          cmd: "editConsignmentGoods",
          shopId: wx.getStorageSync('shopId'),
          userId: wx.getStorageSync('userId'),
          type: type,
          goodsId: that.data.goodsId,
          goodsName: that.data.goodsValName,
          goodsCoverImg: that.data.imgURL,
          goodsTypeId: that.data.shuxing,
          pfgtypeId2: that.data.erjiFenlei,
          unitNo: that.data.danweis,
          townId: JSON.stringify(tlist),
          goodsDesc: that.data.xiangQingimg,
          agtratio: that.data.shouyibili,
          gactBean: JSON.stringify(gActBean),
          skuBean: JSON.stringify(that.data.guigelist),
          goodsImg: JSON.stringify(that.data.goodsZhutu)
        },
        header: {
          "content-type": "application/x-www-form-urlencoded",
          "token": wx.getStorageSync('token')
        },
        success:function(ress) {
         
          if (ress.data.result == 0) {
            setTimeout(function() {
              wx.showToast({
                title: ress.data.resultNote,
                icon: 'none',
                duration: 1500
              })
            }, 0)
            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          }else{
            wx.showToast({
              title: ress.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }
          if (ress.data.zjstate == 1) {
            setTimeout(function () {
              wx.hideLoading()
              wx.stopPullDownRefresh() //停止下拉刷新
            }, 0)
            setTimeout(function () {
              wx.showModal({
                title: '您的账号已在其他设备登录，请重新登录！',
                showCancel: false, //是否显示取消按钮false去掉取消按钮
                success: function (resss) {
                  if (resss.confirm) {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  } else {
                    wx.clearStorageSync()
                    wx.reLaunch({
                      url: '/pages/login/login',
                    })
                  }
                }
              })
            }, 0)
            return
          }
         
          
        }
      });

      } else if (res.cancel) {
      console.log('用户点击取消')
      }
      }
      })
   
  },
  // 商品名称
  goodsVal(e) {
    console.log(e)
    this.setData({
      goodsValName: e.detail.value
    })
  },
  // 头部切换
  changeIndex(e) {
    let that = this
    console.log(e.currentTarget.dataset.index)
    that.setData({
      headIndex: e.currentTarget.dataset.index
    })
  },




  // 关闭弹窗*******************************************
  close_dingdanTime1: function() {
    this.setData({
      shopfenlModal: false, //店铺分类
      showAnceng1: false, //暗层的显示
      yijiModel: false,
      erjiModel: false,
      danweiModel: false,
      addshopfenlModal: false
    })
  },
  // 点击店普分类弹窗*****************************************
  showshoplist() {
    let that = this
    if (!that.data.shuxingFenleiarray.length) {
      that.setData({
        showAnceng1: true,
        shopfenlModal: true,
        zanshiName: '',
        anshiId: ''
      })
    } else {
      that.setData({
        showAnceng1: true,
        shopfenlModal: true,
        zanshiName: 0,
        anshiId: that.data.shuxingFenleiarray[0].goodsTypeId
      })
    }

  },
  // 店铺暂时存储
  changeshuxingVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.shuxingFenleiarray[e.detail.value[0]].goodsTypeId
    })
  },
  // 确定店铺属性分类
  shuxingChange: function() {
    let that = this
    that.setData({
      shuxingindex: that.data.zanshiName,
      shuxing: that.data.anshiId,
      showAnceng1: false,
      shopfenlModal: false,
    })
  },
  // 新增分类***********************
  addshuxingss() {
    let that = this
    that.setData({
      showAnceng1: true,
      addshopfenlModal: true,
      shopfenlModal: false,
    })
  },
  // 添加新店铺分类
  iName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  // 添加新店铺分类
  confirmM: function(e) {
    let that = this
    if (!that.data.name) {
      wx.showToast({
        title: '请输入分类',
        icon: 'none'
      })
      return
    }
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "upGoodsTypes",
        shopId: wx.getStorageSync('shopId'),
        name: that.data.name,
        seq: '11',
        state: 0,
        type: 1
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          setTimeout(function() {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1500
            })
          }, 0)
          that.getshoptypelist()
          that.setData({
            addshopfenlModal: false,
            showAnceng1: false,
          })
        }
      }
    });
  },

  // 点击一级分类弹窗******************************************
  showyijilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      yijiModel: true,
      zanshiName: 0,
      anshiId: that.data.sumfenlei[0].pfgTypeId1
    })
  },
  // 一级暂时存储
  changeyijiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.sumfenlei[e.detail.value[0]].pfgTypeId1
    })
  },
  // 确定1级分类
  yijiChange: function() {
    let that = this
    this.setData({
      yijiindex: that.data.zanshiName,
      yijiFenlei: that.data.anshiId,
      showAnceng1: false, //暗层的显示
      yijiModel: false,
      erjiindex: '',
      erjiFenlei: ''
    })
  },
  // 未选一级，选二级******************************
  yesorno() {
    setTimeout(function() {
      wx.showToast({
        title: '请先选择一级分类',
        icon: 'none',
        duration: 1500
      })
    }, 0)
  },
  // 点击二级分类弹窗**********************************
  showerjilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      erjiModel: true,
      zanshiName: 0,
      anshiId: that.data.sumfenlei[that.data.yijiindex].subTypeList[0].pfgTypeId2
    })
  },
  // 二级暂时存储
  changeerjiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.sumfenlei[that.data.yijiindex].subTypeList[e.detail.value[0]].pfgTypeId2
    })
  },
  // 确定二级分类
  erjiChange: function(e) {
    let that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      erjiindex: that.data.zanshiName,
      erjiFenlei: that.data.anshiId,
      showAnceng1: false,
      erjiModel: false,
    })
  },

  // 点击单位弹窗******************************************************************
  showdanweilist() {
    let that = this
    that.setData({
      showAnceng1: true,
      danweiModel: true,
      zanshiName: 0,
      anshiId: that.data.array[0].unitNo
    })
  },
  // 单位暂时存储
  changedanweiVal(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this
    that.setData({
      zanshiName: e.detail.value[0],
      anshiId: that.data.array[e.detail.value[0]].unitNo
    })
  },
  //确定单位
  danweiChange: function(e) {
    let that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      index: that.data.zanshiName,
      danweis: that.data.anshiId,
      showAnceng1: false,
      danweiModel: false,
    })
  },





  // 获取商品一级二级分类
  getgoodstypelist() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getPlatGoodType"
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        // setTimeout(function () {
        //   wx.hideLoading()
        // }, 0)
        // setTimeout(function () {
        //   wx.showToast({
        //     title: res.data.resultNote,
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }, 0)
        console.log(res, 12)
        that.setData({
          sumfenlei: res.data.pfgType
        })
        if (that.data.goodsId) {
          setTimeout(function() {
            wx.showLoading({
              title: '加载中',
              mask: true
            })
            that.getgoodsinfo()
          }, 500)
        }
        // if (res.data.result == 0) {
        //   setTimeout(function () {
        //     wx.navigateBack({
        //       delta: 1
        //     })
        //   }, 1500)
        // }
      }
    });
  },
  // 上传商品主图
  addzhutuImg() {
    let that = this
    that.setData({
      imglistobg: ''
    })
    wx.navigateTo({
      url: '/pages/setting/page/cropper/cropper?type=zhu',
    })
  },
  // 删除商品主图
  shanchuzhutu(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.goodsZhutu
    tupianList.splice(indexs, 1)
    this.setData({
      goodsZhutu: tupianList
    })
  },
  xiangqTu() {
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log(res)
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        if (res.tempFiles[0].size > 1000000) {
          wx.showToast({
            title: '图片大于1M，请重新选择！',
            icon: 'none'
          })
          return
        } else {
          setTimeout(function() {
            wx.showLoading({
              title: '上传中',
            })
          }, 0)
          wx.request({
            url: app.globalData.myurl,
            method: 'POST',
            data: {
              cmd: "uploadImg",
              imgFile: wx.getFileSystemManager().readFileSync(tempFilePaths, "base64"),
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              that.setData({
                xiangQingimg: res.data.imgUrl
              })
              setTimeout(function() {
                wx.hideLoading()
              }, 0)
              setTimeout(function() {
                wx.showToast({
                  title: res.data.resultNote,
                  icon: 'none',
                  duration: 1500
                })
              }, 0)
            }
          });
        }

      }
    })
  },
  // 删除商品规格
  shanchuguige(e) {
    let indexs = e.currentTarget.dataset.index
    let tupianList = this.data.guigelist
    tupianList.splice(indexs, 1)
    this.setData({
      guigelist: tupianList
    })
  },
  getgoodsinfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "consignmentGoodsDetails",
        shopId: wx.getStorageSync('shopId'),
        goodsId: that.data.goodsId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let danweiindex //单位
          for (var index1 in that.data.array) {
            if (that.data.array[index1].unit == res.data.goodsUnit) {
              danweiindex = that.data.array[index1].unitNo
            }
          }
          // 店铺属性分类
          let shuxiangfenleiindex = 0
          for (let i in that.data.shuxingFenleiarray) {
            if (that.data.shuxingFenleiarray[i].goodsTypeId == res.data.typeId) {
              shuxiangfenleiindex = i
            }
          }
          // 一级分类、二级分类
          let yijiindex = 0
          let erjiindex = 0
          for (let j in that.data.sumfenlei) {
            let obj = that.data.sumfenlei[j]
            if (obj.pfgTypeId1 == res.data.pfgId1) {
              yijiindex = j
              for (let k in obj.subTypeList) {
                if (obj.subTypeList[k].pfgTypeId2 = res.data.pfgId2) {
                  erjiindex = k
                }
              }
            }
          }
          // 销售区域
          let salesArea = res.data.salesArea
          let salesArealist = []
          for (let l in salesArea) {
            salesArealist.push({
              town: salesArea[l],
              townId: l,
              actives: true
            })
          }
          // if (res.data.state == 0) {
          //   // 日期
          //   let year1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getFullYear()
          //   let month1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getMonth() + 1
          //   let day1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getDate()
          //   let hour1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getHours()
          //   let minute1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getMinutes()
          //   let second1 = (new Date(res.data.act.endTtime.replace(/-/g, '/'))).getSeconds()

          //   let year2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getFullYear()
          //   let month2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getMonth() + 1
          //   let day2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getDate()
          //   let hour2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getHours()
          //   let minute2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getMinutes()
          //   let second2 = (new Date(res.data.act.saleeTime.replace(/-/g, '/'))).getSeconds()

          //   let year3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getFullYear()
          //   let month3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getMonth() + 1
          //   let day3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getDate()
          //   let hour3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getHours()
          //   let minute3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getMinutes()
          //   let second3 = (new Date(res.data.act.accteTime.replace(/-/g, '/'))).getSeconds()

          //   that.setData({
          //     goodsValName: res.data.goodsName,
          //     danweis: danweiindex,
          //     index: danweiindex,
          //     imgURL: res.data.goodsCoverImg,
          //     shouyibili: res.data.agtratio,
          //     actId: res.data.act.actId,
          //     guigelist: res.data.goodSkuList,
          //     goodsZhutu: res.data.goodsImg,
          //     xiangQingimg: res.data.goodsDesc,
          //     year1: year1,
          //     year2: year2,
          //     year3: year3,
          //     month1: month1,
          //     month2: month2,
          //     month3: month3,
          //     day1: day1,
          //     day2: day2,
          //     day3: day3,
          //     hour2: hour2,
          //     hour1: hour1,
          //     hour3: hour3,
          //     minute2: minute2,
          //     minute1: minute1,
          //     minute3: minute3,
          //     second2: second2,
          //     second1: second1,
          //     second3: second3,
          //     startTimestr: res.data.act.endTtime,
          //     endTimestr: res.data.act.saleeTime,
          //     sumEndTimestr: res.data.act.accteTime
          //   })
          // } else {
          that.setData({
            goodsValName: res.data.goodsName,
            danweis: danweiindex,
            index: danweiindex,
            imgURL: res.data.goodsCoverImg,
            shouyibili: res.data.agtratio,
            actId: res.data.act.actId,
            guigelist: res.data.goodSkuList,
            goodsZhutu: res.data.goodsImg,
            xiangQingimg: res.data.goodsDesc,
            qibunum: res.data.minbuynum,


            startTimestr: res.data.act.endTtime,
            endTimestr: res.data.act.saleeTime,
            sumEndTimestr: res.data.act.accteTime,
            shuxingindex: shuxiangfenleiindex,
            shuxing: that.data.shuxingFenleiarray[shuxiangfenleiindex].goodsTypeId, //属性ID
            yijiindex: yijiindex,
            yijiFenlei: that.data.sumfenlei[yijiindex].pfgTypeId1,
            erjiindex: erjiindex,
            erjiFenlei: that.data.sumfenlei[yijiindex].subTypeList[erjiindex].pfgTypeId2,
            region: salesArealist
          })
          // }
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    let dates = new Date()
    that.setData({
      year1: dates.getFullYear(),
      year2: dates.getFullYear(),
      year3: dates.getFullYear(),
      month1: dates.getMonth() + 1,
      month2: dates.getMonth() + 1,
      month3: dates.getMonth() + 1,
      day1: dates.getDate(),
      day2: dates.getDate(),
      day3: dates.getDate(),
      hour1: dates.getHours(),
      hour2: dates.getHours(),
      hour3: dates.getHours(),
      minute1: dates.getMinutes(),
      minute2: dates.getMinutes(),
      minute3: dates.getMinutes(),
      second1: dates.getSeconds(),
      second2: dates.getSeconds(),
      second3: dates.getSeconds(),
      valueRiqi: [dates.getFullYear() - 1990, dates.getMonth(), dates.getDate() - 1],
      valueShijian: [dates.getHours(), dates.getMinutes(), dates.getSeconds()],
    })
    if (options.goodsId) {
      that.setData({
        goodsId: options.goodsId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    let goodsZhutu = that.data.goodsZhutu
    if (that.data.imglistobg) {
      goodsZhutu.push(that.data.imglistobg)
    }
    that.setData({
      goodsZhutu: goodsZhutu
    })
    that.getdanwei()
    that.getshoptypelist()
    that.getgoodstypelist()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    let that = this
    that.setData({
      imglistobg: ''
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    let that = this
    that.setData({
      imglistobg: ''
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '众商联采',
      path: "/pages/login/login", // 路径，传递参数到指定页面。
      // imageUrl:'../../../images/logo/LOGO.png'
      imageUrl: app.globalData.logo
    }
  }
})